package com.lenovo.agricultureServer;

import java.net.UnknownHostException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.lenovo.agricultureServer.Serial.SerialportService;
import com.lenovo.agricultureServer.http.HTTPServerService;
import com.lenovo.agricultureServer.util.Util;

/**
 * 主页面
 * 
 * @author user_ygl
 * 
 */
public class MainActivity extends Activity 
{
	private static final String TAG = "MainActivity";
	TextView ip;
	Switch serverSwitch,simulate_switch;
	Button setup;
	RelativeLayout server_bg_lay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG,"onCreate");
		setContentView(R.layout.activity_main);
		ip = (TextView) findViewById(R.id.ip);
		serverSwitch = (Switch) findViewById(R.id.server_switch);
		simulate_switch= (Switch) findViewById(R.id.simulate_switch);
		server_bg_lay = (RelativeLayout) findViewById(R.id.server_bg_lay);

		//server_bg_lay.setBackgroundResource(R.drawable.bg);
		server_bg_lay.setBackgroundResource(R.drawable.server_bg);
		
		setup = (Button) findViewById(R.id.setup);
		try {
			ip.setText("I P 地址：" + getLocalIpAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		if(Util.isServiceExisted(MainActivity.this, HTTPServerService.class.getName())){
			serverSwitch.setChecked(true);
		} else {
			serverSwitch.setChecked(false);
		}

		serverSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Intent sev = new Intent(MainActivity.this,
						HTTPServerService.class);

				if (isChecked) {
					if(!Util.isServiceExisted(MainActivity.this, HTTPServerService.class.getName()))
					{
						SerialportService.ifSIMULATE=false;
						startService(sev);
						sev = new Intent(MainActivity.this, SerialportService.class);// 打开http服务
						startService(sev);// 启动串口设备
					}
				} else {
					stopService(sev);
					sev = new Intent(MainActivity.this, SerialportService.class);// 关闭http服务
					stopService(sev);// 关闭串口
				}

			}
		});
		
		simulate_switch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
			 

				if (isChecked) {
					SerialportService.ifSIMULATE=true;
				} else {
					SerialportService.ifSIMULATE=false;
				}

			}
		});
		
		setup.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog1(chose);
			}
		});
	}

	public static int chose = 0;

	/* Get ip address */
	private String getLocalIpAddress() throws UnknownHostException {
		WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		String IP;
		int ipAddress = wifiInfo.getIpAddress();
		IP = intToIp(ipAddress);
		return IP;
	}

	// ip地址转化成字符串
	private String intToIp(int i) {
		return (i & 0xFF) + " . " + ((i >> 8) & 0xFF) + " . "
				+ ((i >> 16) & 0xFF) + " . " + ((i >> 24) & 0xFF);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG,"onDestroy");
		/*Intent sev = new Intent(MainActivity.this, HTTPServerService.class);
		stopService(sev);
		sev = new Intent(MainActivity.this, SerialportService.class);
		stopService(sev);*/
	}

	// 定义串口设备列表
	public static String[] devices = new String[] { "/dev/ttySAC0",
			"/dev/ttySAC1", "/dev/ttySAC2", "/dev/ttySAC3" };

	// private String[] devicesName = new String[] { "COM0", "COM1", "COM2",
	// "COM3" };

	// 弹出设备选择列表界面
	private void showDialog1(int index) {

		// new AlertDialog.Builder(this)
		// .setTitle("请选择设备")
		// .setIcon(android.R.drawable.ic_dialog_info)
		// .setSingleChoiceItems(devicesName, index,
		// new DialogInterface.OnClickListener() {
		// public void onClick(DialogInterface dialog,
		// int which) {
		// chose = which;
		//
		// Toast.makeText(MainActivity.this,
		// "选择了" + devicesName[chose] + "",
		// Toast.LENGTH_SHORT).show();
		// dialog.dismiss();
		// }
		// }).setNegativeButton("取消", null).show();

		final AlertDialog myDialog = new AlertDialog.Builder(MainActivity.this)
				.create();
		myDialog.show();
		myDialog.getWindow().setContentView(R.layout.dialog);
		myDialog.getWindow().findViewById(R.id.close)
				.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						myDialog.dismiss();
					}
				});

		RelativeLayout lay0 = (RelativeLayout) myDialog.getWindow().findViewById(
				R.id.lay0);
		ImageView img0 = (ImageView) myDialog.getWindow().findViewById(
				R.id.img0);
		TextView text0 = (TextView) myDialog.getWindow().findViewById(
				R.id.text0);

		RelativeLayout lay1 = (RelativeLayout) myDialog.getWindow().findViewById(
				R.id.lay1);
		ImageView img1 = (ImageView) myDialog.getWindow().findViewById(
				R.id.img1);
		TextView text1 = (TextView) myDialog.getWindow().findViewById(
				R.id.text1);

		RelativeLayout lay2 = (RelativeLayout) myDialog.getWindow().findViewById(
				R.id.lay2);
		ImageView img2 = (ImageView) myDialog.getWindow().findViewById(
				R.id.img2);
		TextView text2 = (TextView) myDialog.getWindow().findViewById(
				R.id.text2);

		RelativeLayout lay3 = (RelativeLayout) myDialog.getWindow().findViewById(
				R.id.lay3);
		ImageView img3 = (ImageView) myDialog.getWindow().findViewById(
				R.id.img3);
		TextView text3 = (TextView) myDialog.getWindow().findViewById(
				R.id.text3);

		lay0.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				 chose = 0;
				myDialog.dismiss();
			}
		});
		lay1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				 chose = 1;
				myDialog.dismiss();
			}
		});
		lay2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				 chose = 2;
				myDialog.dismiss();
			}
		});
		lay3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				 chose = 3;
				myDialog.dismiss();
			}
		});
		
		switch (index) {
		case 0:
			img0.setVisibility(View.VISIBLE);
			img1.setVisibility(View.GONE);
			img2.setVisibility(View.GONE);
			img3.setVisibility(View.GONE);
			
			text0.setTextColor(0xffb30c01);
			text1.setTextColor(0xff6c6c6c);
			text2.setTextColor(0xff6c6c6c);
			text3.setTextColor(0xff6c6c6c);
			break;
		case 1:
			img0.setVisibility(View.GONE);
			img1.setVisibility(View.VISIBLE);
			img2.setVisibility(View.GONE);
			img3.setVisibility(View.GONE);
			
			text0.setTextColor(0xff6c6c6c);
			text1.setTextColor(0xffb30c01);
			text2.setTextColor(0xff6c6c6c);
			text3.setTextColor(0xff6c6c6c);
			break;
		case 2:
			img0.setVisibility(View.GONE);
			img1.setVisibility(View.GONE);
			img2.setVisibility(View.VISIBLE);
			img3.setVisibility(View.GONE);
			
			text0.setTextColor(0xff6c6c6c);
			text1.setTextColor(0xff6c6c6c);
			text2.setTextColor(0xffb30c01);
			text3.setTextColor(0xff6c6c6c);
			break;
		case 3:
			img0.setVisibility(View.GONE);
			img1.setVisibility(View.GONE);
			img2.setVisibility(View.GONE);
			img3.setVisibility(View.VISIBLE);
			
			text0.setTextColor(0xff6c6c6c);
			text1.setTextColor(0xff6c6c6c);
			text2.setTextColor(0xff6c6c6c);
			text3.setTextColor(0xffb30c01);
			break;

		default:
			break;
		}

	}

}
